/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.gui.wizard.impl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.SwingWorker;

import org.apache.log4j.Logger;

import edu.drexel.sd0910.ece01.aqmon.data.ActiveNode;
import edu.drexel.sd0910.ece01.aqmon.gui.MainModel;
import edu.drexel.sd0910.ece01.aqmon.gui.wizard.WizardPanelDescriptor;

/**
 * Wizard Descriptor - Summary panel. This is the fifth panel shown during the
 * setup wizard. See
 * {@link edu.drexel.sd0910.ece01.aqmon.gui.wizard.impl.SummaryPanel
 * SummaryPanel}.
 * 
 * @author Kyle O'Connor
 * 
 */
public class SummaryPanelDescriptor extends WizardPanelDescriptor implements ActionListener {
	
	private final static Logger log = Logger.getLogger(SummaryPanelDescriptor.class);

	public static final String IDENTIFIER = "SUMMARY_PANEL";

	private SummaryPanel panel;

	public SummaryPanelDescriptor(MainModel model) {
		panel = new SummaryPanel(model);
		this.setPanelIdentifier(IDENTIFIER);
		this.setTargetPanel(panel);
		panel.addDisableTerminalListener(this);
	}

	@Override
	public void aboutToDisplayPanel() {
		getWizard().setNextFinishButtonEnabled(false);
		panel.toggleTerminalOutputVisible();
		displayNodeInfo();
		initServerConnection();
	}

	@Override
	public Object getBackPanelDescriptor() {
		return MapPanelDescriptor.IDENTIFIER;
	}

	@Override
	public Object getNextPanelDescriptor() {
		return FINISH;
	}
	
	public void setGuiElementsEnabled(boolean state) {
		panel.setDeploymentGuiElementsEnabled(state);
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		panel.toggleTerminalOutputVisible();
	}
	
	private void displayNodeInfo() {
		new SwingWorker<Void, Void>() {

			@Override
			protected Void doInBackground() throws Exception {
					panel.displayPort();
					List<ActiveNode> list = panel.model.getXbeeManager().getActiveNodesList();
					synchronized (list) {
						Iterator<ActiveNode> i = list.iterator();
						while (i.hasNext()) {
							panel.displayNode(i.next());
						}
					}
					panel.displayNodeFinished();
				return null;
			}
		}.execute();
	}
	
	private void initServerConnection() {
		new SwingWorker<Void, Void>() {

			@Override
			protected Void doInBackground() throws Exception {
				try {
					panel.model.getFileUploadManager().connect();
					panel.displayDeploymentNames(panel.model.getFileUploadManager().listDirectories());
				} catch (Exception e) {
					log.error(e.toString());
				}
				panel.displayServerStatus();
				getWizard().setNextFinishButtonEnabled(true);
				return null;
			}
		}.execute();
	}

}
